<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单</title>
</head>
<body>
    <h1>我的订单</h1>
    <table>
        <tbody id="orders"></tbody>
    </table>
    <script src="./js/jquery-1.12.4.min.js"></script>
    <script>
        window.onload = function(){
            
            getUser();
        }
        function getUser(){
            $.ajax({
                url:"/api/shop-user/users/token",
                method:"get",
                success(data){
                    let userId = data.data.userId;
                    if(!userId){
                        alert("请先登录");
                        
                    }else{
                        getOrders(userId);
                    }
                }
            })
        }
        function pay(orderId,status){
            console.log("status:",status)
            if(status == 0){
                $.ajax({
                    url:"/api/shop-pay/pay/"+orderId,
                    method:"get",
                    success(){
                        alert("支付成功");
                    }
                })
            }else{
                alert("已支付");
            }
        }
        function getOrders(userId){
            // 获取当前用户下面的所有订单
            $.ajax({
                url:"/api/shop-order/orders/users/"+userId,
                method:"get",
                success(data){
                    console.log(data);
                    $(data.data).each(function(index,order){
                        console.log("order",order);
                        $("#orders").append(`
                            <tr>
                                <td>${order.orderId}</td>
                                <td>${order.createTime}</td>                               
                                <td>${order.status}</td>
                                <td><input type="button" value="支付" onclick="pay('${order.orderId}',${order.status})"></td>
                            </tr>
                        `);
                    })
                   
                }
            })
        }
    </script>
</body>
</html>